package com.tjsinfo.tjsoa.articles.action;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.json.JSONArray;
import org.json.JSONObject;

import com.tjsinfo.tjsoa.articles.bo.IArticlesBO;
import com.tjsinfo.tjsoa.articles.bo.ManageSuperAction;
import com.tjsinfo.tjsoa.articles.bo.TArticlesRolesManage;
import com.tjsinfo.tjsoa.articles.vo.TIndexArticles;
import com.tjsinfo.tjsoa.common.SessionUser;
import com.tjsinfo.tjsoa.common.DayFormat;
import com.tjsinfo.tjsoa.reminded.bo.TSystemRemindedManager;
import com.tjsinfo.tjsoa.resource.duty.bo.TResourceDutyManager;
import com.tjsinfo.tjsoa.resource.duty.vo.TResourceDuty;
import com.tjsinfo.tjsoa.system.vo.User;
import com.tjsinfo.tjsoa.articles.bo.TArticlesUserManage;

public class SecondAction extends ManageSuperAction {
	private IArticlesBO articlesBO;
	private String loginName;
	private TArticlesUserManage articlesUserManage;
	private TArticlesRolesManage articlesRolesManage;
	TSystemRemindedManager systemRemindedManager;
	private TResourceDutyManager resourceDutyManager;
	
	public IArticlesBO getArticlesBO() {
		return articlesBO;
	}

	public void setArticlesBO(IArticlesBO articlesBO) {
		this.articlesBO = articlesBO;
	}
	
	public String getLoginName() {
		return loginName;
	}

	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}
	
	public TArticlesUserManage getArticlesUserManage() {
		return articlesUserManage;
	}

	public void setArticlesUserManage(TArticlesUserManage articlesUserManage) {
		this.articlesUserManage = articlesUserManage;
	}
	
	public TArticlesRolesManage getArticlesRolesManage() {
		return articlesRolesManage;
	}

	public void setArticlesRolesManage(TArticlesRolesManage articlesRolesManage) {
		this.articlesRolesManage = articlesRolesManage;
	}

	public TSystemRemindedManager getSystemRemindedManager() {
		return systemRemindedManager;
	}

	public void setSystemRemindedManager(
			TSystemRemindedManager systemRemindedManager) {
		this.systemRemindedManager = systemRemindedManager;
	}
	
	public TResourceDutyManager getResourceDutyManager() {
		return resourceDutyManager;
	}

	public void setResourceDutyManager(TResourceDutyManager resourceDutyManager) {
		this.resourceDutyManager =resourceDutyManager;
	}


	@SuppressWarnings("unchecked")
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// TODO Auto-generated method stub		
		String loginname = "";
		Integer userid = 1;
		User user = SessionUser.getUser(request);
		if(null != user){
		userid = user.getUser_id();
		loginname = user.getUsername();
		}
		request.setAttribute("userid", String.valueOf(userid));
		request.setAttribute("loginname", loginname);	
			
//hanzhuo -start
 		Calendar c = Calendar.getInstance();
 		DateFormat sdf = new SimpleDateFormat("yyyy-M-d");
 		Date date = c.getTime();
 		Date Date = DayFormat.afterNDay(date, 1);
 		String dutydate = sdf.format(date);
 		String dutydt = sdf.format(Date);
 		String zhibantoday = "";
 		String zhibantomr = "";
 		String[] today={};
 		String[] tomr={};
 		if(null == resourceDutyManager.findByDate(dutydate)){
 			zhibantoday = "无";	
 			today=insert(today,zhibantoday);
 		}else{
 			List<TResourceDuty> todayList=resourceDutyManager.findByProperty("dutyDate", dutydate);
 			for(int i=0;i<todayList.size();i++){
 				zhibantoday=todayList.get(i).getName();
 				today=insert(today,zhibantoday);
 			}
//			zhibantoday=resourceDutyManager.findByDate(dutydate).getName();	
 		} 
 		request.setAttribute("today", today );
		if(null == resourceDutyManager.findByDate(dutydt)){
			zhibantomr = "无";	
			tomr=insert(tomr,zhibantomr);
		}else{
			List<TResourceDuty> tomrList=resourceDutyManager.findByProperty("dutyDate", dutydt);
			for(int i=0;i<tomrList.size();i++){
				zhibantomr=tomrList.get(i).getName();
				tomr=insert(tomr,zhibantomr);
			}
		}
		request.setAttribute("tomr", tomr);
 		

		String path = request.getContextPath();
		String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path;
 		List<TIndexArticles> xinwentupianPicList=articlesBO.findNewsPictures(991,4);
		JSONArray xinwentupianArray	 = new JSONArray();
		String picurl="";
		for (TIndexArticles articlesL : xinwentupianPicList) {
			JSONObject object = new JSONObject();
			String Pic=articlesL.getSmallPic();
			if(Pic.contains(basePath)){
				picurl=Pic;
			}else{
				picurl=basePath + Pic;
			}
			object.put("p", picurl);
			String strUrl= request.getContextPath()+"/tjsinfo/tjsoa/articles/List.do?method=viewArticle&uuid="+articlesL.getUuid().toString();
			object.put("l", strUrl);
			object.put("t", articlesL.getTitle());
			object.put("u", "_parent");
			xinwentupianArray.put(object);
		}
 		request.setAttribute("xinwentupianArray", xinwentupianArray);

 		String zyxw="";
 		String jtdt="";
 		String dzxw="";
 		zyxw=articlesRolesManage.findById(991).getName();
 		jtdt=articlesRolesManage.findById(992).getName();
 		dzxw=articlesRolesManage.findById(993).getName();
 		request.setAttribute("zyxw", zyxw);
 		request.setAttribute("jtdt", jtdt);
 		request.setAttribute("dzxw", dzxw);
 		
 		List<TIndexArticles> zhongyaoxinwenList=articlesBO.findInexByCid(991,5);
 		request.setAttribute("zhongyaoxinwenList", zhongyaoxinwenList);
 		
 		List<TIndexArticles> zxxxPicList=articlesBO.findNewsPictures(991,4);
		request.setAttribute("zxxxPicList", zxxxPicList);
		
		List<TIndexArticles> tupianxinwenPicList=articlesBO.findNewsPictures(991,12);
		request.setAttribute("tupianxinwenPicList", tupianxinwenPicList);
 		
 		List<TIndexArticles> jituandongtaiList=articlesBO.findInexByCid(992,7);
 		request.setAttribute("jituandongtaiList", jituandongtaiList);
 		
 		List<TIndexArticles> jituandongtaiPicList=articlesBO.findNewsPictures(992);
 		Integer a=jituandongtaiPicList.size();
        Integer b=a;
 		request.setAttribute("jituandongtaiPicList", jituandongtaiPicList);
 		
 		List<TIndexArticles> dangzhengxinwenList=articlesBO.findInexByCid(993,7);
 		request.setAttribute("dangzhengxinwenList", dangzhengxinwenList);
 
 		List<TIndexArticles> dangzhengxinwenPicList=articlesBO.findNewsPictures(993);
 		request.setAttribute("dangzhengxinwenPicList", dangzhengxinwenPicList);
 		
//_end		
		
		return mapping.findForward("ok");
	}
 
	public static String[] insert(String[] arr, String str)
	   {
	   int size = arr.length;
	   
	   String[] tmp = new String[size + 1];
	   
	   System.arraycopy(arr, 0, tmp, 0, size);
	   
	   tmp[size] = str;
	   
	   return tmp;
	   }
}
